package leetcode.code1261;

import java.util.HashSet;
import java.util.Set;

import leetcode.helper.tree.TreeNode;

class FindElements {

	Set<Integer> set;

	public FindElements(TreeNode root) {
		set = new HashSet<>();
		root.val = 0;
		set.add(0);
		fix(root);
	}

	private void fix(TreeNode root) {
		if (root.left != null) {
			int val = (root.val << 1) + 1;
			this.set.add(val);
			root.left.val = val;
			this.fix(root.left);
		}
		if (root.right != null) {
			int val = (root.val << 1) + 2;
			this.set.add(val);
			root.right.val = val;
			this.fix(root.right);
		}
	}

	public boolean find(int target) {
		return this.set.contains(target);
	}
}